package mo.daliy;

public class L3122 {

    public int minimumOperations(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        int[][] dp = new int[n][10];

        for (int i = 0; i < n; i++) {
            for (int p = 0; p < 10; p++) {
                dp[i][p] = Integer.MAX_VALUE;
                int cost = 0;
                for (int j = 0; j < m; j++) {
                    if (grid[j][i] != p) {
                        cost++;
                    }
                }
                if (i == 0) {
                    dp[i][p] = cost;
                } else {
                    for (int q = 0; q < 10; q++) {
                        if (p == q) {
                            continue;
                        }
                        dp[i][p] = Math.min(dp[i][p], dp[i-1][q]+cost);
                    }
                }
            }
        }

        int ret = Integer.MAX_VALUE;
        for (int p = 0; p < 10; p++) {
            ret = Math.min(ret, dp[n-1][p]);
        }
        return ret;
    }

}
